﻿using habbo.Habbo.Characters;
using habbo.Kernel.Commons;
using habbo.Kernel.Network;
using habbo.Kernel.Packets.com.sulake.habbo.communication.messages.outgoing.messenger;
using habbo.Kernel.Storage.Querys;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace habbo.Habbo.Messenger
{
    class MessengerManager
    {
        /// <summary>
        /// Default value of maximal friends.
        /// </summary>
        public short MAX_FRIENDS_DEFAULT { get; private set; }

        /// <summary>
        /// Club members value of maximal friends.
        /// </summary>
        public short MAX_FRIENDS_CLUB { get; private set; }

        /// <summary>
        /// Vip members value of maximal friends.
        /// </summary>
        public short MAX_FRIENDS_VIP { get; private set; }

        /// <summary>
        /// Maximal friends per page.
        /// </summary>
        public short MAX_FRIENDS_PER_PAGE { get; private set; }

        /// <summary>
        /// Update for the messenger
        /// </summary>
        public ICollection<MessengerUpdate> UpdateRequests { get; private set; }

        public MessengerManager()
        {
            this.MAX_FRIENDS_DEFAULT = (short)1100;
            this.MAX_FRIENDS_CLUB = (short)300;
            this.MAX_FRIENDS_VIP = (short)800;
            this.MAX_FRIENDS_PER_PAGE = (short)1100;

            this.UpdateRequests = new List<MessengerUpdate>();
        }

        /// <summary>
        /// Returns an collection of messengerfriends
        /// </summary>
        /// <param name="CharacterId"></param>
        /// <returns></returns>
        public ICollection<int> GetMessengerFriends(int CharacterId)
        {
            ICollection<int> Output = new List<int>();

            foreach (DataRow Row in SystemApp.MySQLManager.GetObject(new MessengerFriendsQuery(CharacterId)).GetOutput<DataTable>().Rows)
            {
                using (RowAdapter Adapter = new RowAdapter(Row))
                {
                    int xCharacterId = Adapter.PopInt32("character_id");
                    int xFriendId = Adapter.PopInt32("friend_id");

                    if (xCharacterId == CharacterId)
                    {
                        if (!Output.Contains(xFriendId))
                        {
                            Output.Add(xFriendId);
                        }
                    }
                    else
                    {
                        if (!Output.Contains(xCharacterId))
                        {
                            Output.Add(xCharacterId);
                        }
                    }
                }
            }

            return Output;
        }

        
        public void ParseStatus(int CharacterId)
        {
            foreach (int FriendId in GetOnlineMessengerFriends(CharacterId))
            {
                Session Friend = SystemApp.SocketSystem.GetSession(FriendId);
                FriendListUpdateComposer fluc = new FriendListUpdateComposer(Friend, new Dictionary<int, int>
                {
                    {
                        CharacterId, (int) MessengerUpdateType.EnterOrLeave
                    }
                });
            }
        }

        /// <summary>
        /// Returns an collection of online messengerfriends
        /// </summary>
        /// <param name="CharacterId"></param>
        /// <returns></returns>
        public ICollection<int> GetOnlineMessengerFriends(int CharacterId)
        {
            ICollection<int> Output = new List<int>();

            foreach (int FriendId in GetMessengerFriends(CharacterId))
            {
                if (SystemApp.HabboSystem.CharacterManager.GetStatus(FriendId) == CharacterStatus.Online)
                {
                    Output.Add(FriendId);
                }
            }

            return Output;
        }

        /// <summary>
        /// Returns my friends updates.
        /// </summary>
        /// <param name="CharacterId"></param>
        /// <returns></returns>
        public Dictionary<int, int> GetUpdateRequests(int CharacterId)
        {
            Dictionary<int, int> Output = new Dictionary<int, int>();
            ICollection<MessengerUpdate> ToRemove = new List<MessengerUpdate>();

            foreach (MessengerUpdate Update in UpdateRequests)
            {
                if (Update.TargetId == CharacterId)
                {
                    Output.Add(Update.UpdateId, (int)Update.Mode);
                }
            }

            foreach (MessengerUpdate Update in ToRemove)
            {
                UpdateRequests.Remove(Update);
            }

            return Output;
        }
    }
}
